home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261exe.zip / ps2image.ps < prev    next >
Text File  |  1993-05-21  |  6KB  |  190 lines

  1. %    Copyright (C) 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2. %
  3. % This file is part of Ghostscript.
  4. %
  5. % Ghostscript is distributed in the hope that it will be useful, but
  6. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. % to anyone for the consequences of using it or for whether it serves any
  8. % particular purpose or works at all, unless he says so in writing.  Refer
  9. % to the Ghostscript General Public License for full details.
  10. %
  11. % Everyone is granted permission to copy, modify and redistribute
  12. % Ghostscript, but only under the conditions described in the Ghostscript
  13. % General Public License.  A copy of this license is supposed to have been
  14. % given to you along with Ghostscript so you can know your rights and
  15. % responsibilities.  It should be in a file named COPYING.  Among other
  16. % things, the copyright notice and this notice must be preserved on all
  17. % copies.
  18.  
  19. % Convert a .ps file to another .ps file containing only a bit image.
  20. % After loading ps2image.ps into Ghostscript, invoke
  21. %    (output_file.ps) ps2image
  22. % This replaces the current device, writing the output on the file
  23. % instead of to the device.  Then invoke
  24. %    (your_input_file.ps) run
  25. % To display the image at a later time, simply run the file that was
  26. % written (output_file.ps).
  27.  
  28. % Initialize, and redefine copypage and showpage.
  29. /ps2idict 25 dict def
  30. ps2idict begin
  31.  
  32.         % Save the showpage operator.
  33.   /realshowpage /showpage load def
  34.  
  35.         % Define a monochrome palette.
  36.   /monopalette <00 ff> def
  37.  
  38.         % Initialize the tables for reading a image.
  39.         % This code gets copied to the output file.
  40.   /initread
  41.    {
  42.         % Initialize the strings for filling runs.
  43.  
  44.      /.ImageFills 31        % max repeat count
  45.      0 1 255
  46.       { 1 index string dup 0 1 5 index 1 sub { 3 index put dup } for
  47.         pop exch pop readonly exch
  48.       } for pop
  49.       256 array astore readonly def
  50.  
  51.         % Initialize the procedure table for input dispatching.
  52.  
  53.      /.ImageProcs
  54.      33 { { pop .ImageItem } } repeat
  55.      32 { {    % 0x21-0x40: (N-0x20) data bytes follow
  56.       32 sub 3 index exch 0 exch getinterval 2 index exch
  57.       readhexstring pop exch pop dup
  58.      } bind } repeat
  59.      31 { {    % 0x41-0x5f: repeat last data byte (N-0x40) times
  60.       64 sub .ImageFills 2 index dup length 1 sub get get
  61.       exch 0 exch getinterval
  62.      } bind } repeat
  63.      160 { { pop .ImageItem } } repeat
  64.      256 array astore readonly def
  65.  
  66.    } def
  67.         % Read one item from a compressed image.
  68.         % This procedure gets copied to the output file.
  69.         % Stack contents: <buffer> <file> <previous>
  70.   /.ImageItem
  71.    { 1 index read pop dup .ImageProcs exch get exec
  72.    } def
  73.         % Read and print an entire compressed image,
  74.         % scaling it uniformly in X and Y to fill the page.
  75.         % This procedure gets copied to the output file.
  76.         % Arguments: <width> <height>
  77.   /.ImageRead
  78.    { gsave 1 [
  79.      clippath pathbbox pop pop translate
  80.      pathbbox newpath 4 -2 roll pop pop
  81.      dup 3 1 roll abs 5 index exch div exch abs 6 index exch div
  82.      2 copy lt { exch } if    % (definition of max)
  83.      0 0 2 index neg 0 4 index 7 -1 roll mul
  84.      ] { .ImageItem }
  85.      4 index 7 add 8 idiv string currentfile ()
  86.      8 3 roll
  87.      image pop pop pop
  88.      grestore showpage
  89.    } def
  90.  
  91.         % Write a repeat command on the file.
  92.   /writerepeat        % <count> writerepeat -
  93.    {  { dup 31 le { exit } if myfile (_) writestring 31 sub } loop
  94.      dup 0 ne { 64 add myfile exch write } { pop } ifelse
  95.    } bind def
  96.  
  97.         % Write a data command on the file.
  98.   /writedata        % <string> writedata -
  99.    {  { dup length 0 eq { exit } if
  100.     dup length 32 min
  101.     dup 16#20 add myfile exch write
  102.     1 index 0 2 index getinterval myfile exch writehexstring
  103.     myfile (\n) writestring
  104.     1 index length 1 index sub getinterval
  105.       }
  106.      loop pop
  107.    } bind def
  108.  
  109.         % Write an entire data string on the file.
  110.   /writedatastring        % <string> writedatastring -
  111.    {  { dup length 4 lt { exit } if
  112.         % Detect a maximal run of non-repeated data.
  113.     dup length 4 sub 0 exch 1 exch
  114.      { 2 copy get
  115.        dup 3 index 3 index 1 add get eq
  116.         { dup 3 index 3 index 2 add get eq
  117.            { dup 3 index 3 index 3 add get eq
  118.               { pop exit
  119.           } if
  120.            } if
  121.         } if
  122.       pop pop
  123.      }
  124.     for
  125.     1 add
  126.     1 index 0 2 index getinterval writedata
  127.     1 index length 1 index sub getinterval
  128.         % Detect a maximal run of repeated data.
  129.         % We know there are at least 3 repeated bytes.
  130.     dup 0 get exch
  131.     dup length exch 3 1 3 index 1 sub
  132.      {        % Stack: <byte> <length> <string> <index>
  133.        2 copy get 4 index ne { 3 -1 roll pop exch exit } { pop } ifelse
  134.      }
  135.     for        % Stack: <byte> <length> <string>
  136.     exch dup writerepeat
  137.     1 index length 1 index sub getinterval
  138.     exch pop
  139.       }
  140.      loop writedata
  141.    } bind def
  142.  
  143.         % The main procedure.
  144.   /ps2image
  145.    {                % Open the file
  146.      (w) file /myfile exch def
  147.      /initread load
  148.       { dup myfile exch write==only
  149.         type dup /arraytype eq exch /packedarraytype eq or
  150.      { (\n) } { ( ) } ifelse myfile exch writestring
  151.       } forall
  152.      { /.ImageItem /.ImageRead }
  153.       { dup myfile exch write==only
  154.         load myfile exch write==only
  155.     myfile ( bind def\n) writestring
  156.       } forall
  157.                     % Get the device parameters
  158.      currentdevice getdeviceprops .dicttomark
  159.      dup /HWSize get aload pop
  160.        /devheight exch def
  161.        /devwidth exch def
  162.      /InitialMatrix get
  163.        /devmatrix exch def
  164.                 % Make a corresponding memory device
  165.      devmatrix devwidth devheight monopalette
  166.      makeimagedevice
  167.      /mydevice exch def
  168.      mydevice setdevice        % (does an erasepage)
  169.      /rowwidth devwidth 7 add 8 idiv def
  170.      /row rowwidth 7 add 8 idiv 8 mul string def    % pad for scanning
  171.                 % Replace the definition of showpage
  172.      userdict /showpage { ps2idict begin myshowpage end } bind put
  173.    } def
  174.                 % Write the image on the file
  175.   /myshowpage
  176.    { myfile devwidth write==only   myfile ( ) writestring
  177.      myfile devheight write==only   myfile ( .ImageRead\n) writestring
  178.                  % Write the hex data
  179.      0 1 devheight 1 sub
  180.       { mydevice exch row 0 rowwidth getinterval copyscanlines
  181.     writedatastring
  182.       } for
  183.      myfile flushfile
  184.      erasepage initgraphics
  185.    } bind def
  186.  
  187. end
  188.  
  189. /ps2image { ps2idict begin ps2image end } bind def
  190.